/**
 * DsoMusic Copyright (C) 2020-2021 Moriafly
 *
 * This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
 * This is free software, and you are welcome to redistribute it
 * under certain conditions; type `show c' for details.
 *
 * The hypothetical commands `show w' and `show c' should show the appropriate
 * parts of the General Public License.  Of course, your program's commands
 * might be different; for a GUI interface, you would use an "about box".
 *
 * You should also get your employer (if you work as a programmer) or school,
 * if any, to sign a "copyright disclaimer" for the program, if necessary.
 * For more information on this, and how to apply and follow the GNU GPL, see
 * <https://www.gnu.org/licenses/>.
 *
 * The GNU General Public License does not permit incorporating your program
 * into proprietary programs.  If your program is a subroutine library, you
 * may consider it more useful to permit linking proprietary applications with
 * the library.  If this is what you want to do, use the GNU Lesser General
 * Public License instead of this License.  But first, please read
 * <https://www.gnu.org/licenses/why-not-lgpl.html>.
 */

package com.dirror.music.util.extensions

/**
 * ArrayList 拓展
 * @author Moriafly
 */

/**
 * List 中下一项
 */
fun <T> ArrayList<T>.next(current: T?): T? {
    if (this.isEmpty() || current == null) {
        return null
    }
    when (val position = this.indexOf(current)) {
        -1 -> return null
        this.lastIndex -> {
            this[0]?.let {
                return it
            }
        }
        else -> {
            this[position + 1]?.let {
                return it
            }
        }
    }
    return null
}

/**
 * List 中上一项
 */
fun <T> ArrayList<T>.previous(current: T?): T? {
    if (this.isEmpty() || current == null) {
        return null
    }
    when (val position = this.indexOf(current)) {
        -1 -> return null
        0 -> {
            this[this.lastIndex]?.let {
                return it
            }
        }
        else -> {
            this[position - 1]?.let {
                return it
            }
        }
    }
    return null
}